package com.wander.nio.chat.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class MyUdpClient {
    public static void main(String[] args) {
        try (DatagramSocket socket = new DatagramSocket()) {
            Scanner scanner = new Scanner(System.in);
            InetAddress address = InetAddress.getByName("127.0.0.1");
            int port = 9999;
            System.out.println("请输入用户名：");
            String username = scanner.nextLine();
            while (true) {
                String input = scanner.nextLine();
                String msg = username + ": " + input;
                DatagramPacket packet = new DatagramPacket(msg.getBytes(StandardCharsets.UTF_8), msg.length(), address, port);
                socket.send(packet);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
